package ObjGitModel

import (
	"arabApi/app/api/models"
	"arabApi/app/api/requests/CRequest"
	"arabApi/app/api/requests/ObjGitRequest"
	"arabApi/pkg/database"
	"arabApi/pkg/helpers"
)

// TpObjJob  项目表
type TpObjJob struct {
	models.BaseModel
	Name    string `gorm:"column:name" db:"name" json:"name" form:"name"` //  项目名称
	Desc    string `gorm:"column:desc" db:"desc" json:"desc" form:"desc"` //  描述
	JobPath string `gorm:"column:job_path" db:"job_path"`                 //  描述
	JobType string `gorm:"column:job_type" db:"job_type"`                 //  描述
	models.CommonTimestampsField
}

func (t TpObjJob) TableName() string {
	return "tp_obj_job"
}

func List(r CRequest.Cpage) ([]TpObjJob, int64) {
	var count int64
	var list []TpObjJob
	database.DB.Model(TpObjJob{}).Count(&count).Limit(r.PageSize).Offset((r.Page - 1) * r.PageSize).Order("id desc").Find(&list)
	return list, count
}

func MsgByid(r int64) TpObjJob {
	var msg TpObjJob

	database.DB.Where("id = ?", r).Find(&msg)
	return msg
}

func Add(r ObjGitRequest.ObjGitRequest) (uint64, error) {
	s := &TpObjJob{}

	if err := helpers.Copy(r, s); err != nil {
		return 0, err
	}
	res := database.DB.Model(s).Create(s)
	return s.ID, res.Error
}

func Edit(r ObjGitRequest.ObjGitRequest) error {
	s := &TpObjJob{}

	if err := helpers.Copy(r, s); err != nil {
		return err
	}
	res := database.DB.Model(s).Where("id = ?", r.ID).Updates(s)
	return res.Error
}
